package com.branches.threads;

public class ThreadTest {
    public static void main(String[] args) {
       runMyThread();
       runThreadStatus();
        interrupted();
    }

    private static void runMyThread() {
        MyThread myThread = new MyThread();
        myThread.start();
    }

    private static void runThreadStatus() {
        Thread t = new Thread(() -> {
            System.out.println("hello");
        });
        System.out.println("start");
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end");
    }

    private static void interrupted() {
        Thread t = new MyThread();
        t.start();
        try {
            Thread.sleep(1); // 暂停1毫秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt(); // 中断t线程
        try {
            t.join(); // 等待t线程结束
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end");
    }
}

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("start new Thread.");
    }
}

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("start new thread!");
    }
}